package sdc.com.petscalendar.fragments;

import java.util.ArrayList;

import sdc.com.petscalendar.R;
import sdc.com.petscalendar.adapters.VetoAdapter;
import sdc.com.petscalendar.objects.Veto;
import sdc.com.petscalendar.utils.Statics;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

public class VetoListFragment extends BaseFragment implements
		View.OnClickListener {
	private ListView mListView;
	private View mBtnCreate, mBtnImport;
	private ArrayList<Veto> mVetos;
	private VetoAdapter mAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initData();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (mView == null) {
			mView = inflater.inflate(R.layout.fragment_vetolist, container,
					false);
		}
		return mView;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		initComponents();
		addListeners();
	}

	private void initComponents() {
		mBtnCreate = getView().findViewById(R.id.btn_create);
		mBtnImport = getView().findViewById(R.id.btn_import);

		mListView = (ListView) getView().findViewById(R.id.list);
		mAdapter = new VetoAdapter(getActivity(), mVetos);
		mListView.setAdapter(mAdapter);
	}

	private void addListeners() {
		mBtnCreate.setOnClickListener(this);
		mBtnImport.setOnClickListener(this);

		mAdapter.setEditListener(new VetoAdapter.OnEditListener() {

			@Override
			public void onEditClicked(int position) {
				Statics.sVeto = mAdapter.getItem(position);
				FragmentTransaction fragmentTransaction = getFragmentManager()
						.beginTransaction();
				fragmentTransaction.replace(R.id.frm_veto,
						new VetoEditFragment());
				fragmentTransaction.addToBackStack(null);
				fragmentTransaction.commit();
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_create:
			Statics.sVeto = null;
			FragmentTransaction fragmentTransaction = getFragmentManager()
					.beginTransaction();
			fragmentTransaction.replace(R.id.frm_veto, new VetoEditFragment());
			fragmentTransaction.addToBackStack(null);
			fragmentTransaction.commit();
			break;
		case R.id.btn_import:

			break;
		default:
			break;
		}
	}

	private void initData() {
		String path = Environment.getExternalStorageDirectory().getPath()
				+ "/petapp_data/photo/";
		mVetos = new ArrayList<Veto>();
		for (int i = 1; i < 11; i++) {
			Veto veto = new Veto();
			veto.setId(i);
			veto.setAddress("43 grande rue");
			veto.setCity("Garches");
			veto.setEmail("test@yahoo.fr");
			veto.setPhone("0979581034");
			veto.setName("Pierre Dupont");
			veto.setZip("059000");
			veto.setAvatar(path + "veto_" + i);
			mVetos.add(veto);
		}
	}

	@Override
	public void onResume() {
		super.onResume();
		if (Statics.sVeto != null) {
			if (Statics.sVeto.getId() > 0) {
				mAdapter.notifyDataSetChanged();
			} else {
				Statics.sVeto.setId(mAdapter.getCount());
				mAdapter.add(Statics.sVeto);
				mAdapter.notifyDataSetChanged();
			}
			Statics.sVeto = null;
		}
	}

}
